package com.java.module.fire.controller.api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.java.common.lang.annotation.Log;
import com.java.common.lang.annotation.ValidateToken;
import com.java.common.lang.domain.AjaxResult;
import com.java.common.lang.domain.entity.User;
import com.java.common.lang.domain.page.Page;
import com.java.common.lang.domain.page.TableDataInfo;
import com.java.common.lang.enums.BusinessTypeEnum;
import com.java.common.service.ApiTokenService;
import com.java.common.utils.WebKit;
import com.java.module.fire.domain.*;
import com.java.module.fire.service.ICommentService;
import com.java.module.fire.service.ILikeService;
import com.java.module.fire.service.IUninterestedService;
import com.java.module.user.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.java.module.fire.service.IPostService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 帖子内容Controller
 * 
 * @author Administrator
 * @date 2025-02-10
 */
@RestController("ApiPostController")
@RequestMapping("/fire/post")
public class PostController {

    @Autowired
    IPostService postService;


    @Autowired
    ApiTokenService apiTokenService;


    @Autowired
    ILikeService likeService;


    @Autowired
    ICommentService commentService;


    @Autowired
    IUserService userService;


    @Autowired
    IUninterestedService uninterestedService;


    /**
     * 新增帖子内容
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("新增帖子")
    @PostMapping
    public Post add(@RequestBody Post post) {
        post.setBreed(0);
        post.setUid(apiTokenService.getLoginUserId());
        //在这里产生一个问题，如果需要消耗次数
        if(post.getDrafts() != null &&  post.getDrafts() == 1 &&  post.getBreed() == 0){
            //在这里消耗次数，置顶时间设置为一天
            User user = userService.selectUserById(apiTokenService.getLoginUserId());
            if(user != null && user.getIsTop()>0){
                user.setIsTop(user.getIsTop()-1);
                post.setIsActivity(1L);
                Date today = new Date();
                Date tomorrow = new Date(today.getTime() + (1000 * 60 * 60 * 24));
                post.setUpdateTime(tomorrow);
                userService.updateUser(user);
            }
        }

        if(post.getPostId() == null){
            postService.insertPost(post);
        }else{
            postService.updatePost(post);
        }



        return post;
    }


    /**
     * 查询帖子内容列表
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("项目介绍")
    @GetMapping("/getTopBanner")
    public List<Post> getTopBanner(Post post) {
        post.setBreed(0);
        post.setIsActivity(1L);
        List<Post> list = postService.selectPostListAll(post);
        return list;
    }

    /**
     * 查询帖子内容列表
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("我的草稿箱")
    @GetMapping("/list")
    public Page<Post> list(Post post) {
        WebKit.startPage();
        post.setBreed(0);

        post.setUid(apiTokenService.getLoginUserId());
        List<Post> list = postService.selectPostList(post);
        return WebKit.getPageData(list);
    }



    /**
     * 查询帖子内容列表
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("广场板块")
    @ValidateToken(isValidate = false)
    @GetMapping("/findList")
    public Page<Post> findList(Post post) {
        //在这里获取当前用户不感兴趣的内容
        post.setBreed(0);
        post.setDrafts(1);
        Uninterested uninterested = new Uninterested();
        if(apiTokenService.getLoginUser() != null){
            uninterested.setUid(apiTokenService.getLoginUserId());
        }


        List<Uninterested> uninterestedList = uninterestedService.selectUninterestedList(uninterested);
        post.setUninterestedList(uninterestedList);
        List<Like> listUser = new ArrayList<>();
        List<Like> listLike = new ArrayList<>();
        List<Like> listComment = new ArrayList<>();
        if(apiTokenService.getLoginUser() != null){
            Like like = new Like();
            like.setType(3L);
            like.setUid(apiTokenService.getLoginUserId());
            listUser = likeService.selectLikeList(like);

            like.setType(1L);
            like.setUid(apiTokenService.getLoginUserId());
            listLike = likeService.selectLikeList(like);

            like.setType(2L);
            like.setUid(apiTokenService.getLoginUserId());
            listComment = likeService.selectLikeList(like);



            if(post.getIsTj() != null){
                User user = userService.selectUserById(apiTokenService.getLoginUserId());
                if(user.getUserLable() != null){
                    post.setUserLable(user.getUserLable());
                }

            }
        }
        WebKit.startPage();
        List<Post> list = postService.selectPostListAll(post);
        for (Post post1 : list) {
            if(listUser.stream()
                    .filter(item->item.getPostId().equals(post1.getUid()))
                    .findAny()
                    .isPresent())
            {
                post1.setIsFollow(1);
            }


            if(listLike.stream()
                    .filter(item->item.getPostId().equals(post1.getPostId()))
                    .findAny()
                    .isPresent())
            {
                post1.setIsLiked(1);
            }


            if(listComment.stream()
                    .filter(item->item.getPostId().equals(post1.getPostId()))
                    .findAny()
                    .isPresent())
            {
                post1.setIsComment(1);
            }
        }
        return WebKit.getPageData(list);
    }



    /**
     * 查询帖子内容列表
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("广场关注")
    @ValidateToken(isValidate = false)
    @GetMapping("/findUserList")
    public Page<Post> findUserList(Post post) {
        post.setDrafts(1);
        post.setBreed(0);
        List<Like> listUser = new ArrayList<>();

        List<Like> listLike = new ArrayList<>();

        List<Like> listComment = new ArrayList<>();
        if(apiTokenService.getLoginUser() != null){
            Like like = new Like();
            like.setType(3L);
            like.setUid(apiTokenService.getLoginUserId());
            listUser = likeService.selectLikeList(like);


            like.setType(1L);
            like.setUid(apiTokenService.getLoginUserId());
            listLike = likeService.selectLikeList(like);

            like.setType(2L);
            like.setUid(apiTokenService.getLoginUserId());
            listComment = likeService.selectLikeList(like);
        }
        WebKit.startPage();
        List<Post> list = postService.selectPostListUserAll(post);
        for (Post post1 : list) {
            if(listUser.stream()
                    .filter(item->item.getPostId().equals(post1.getUid()))
                    .findAny()
                    .isPresent())
            {
                post1.setIsFollow(1);
            }


            if(listLike.stream()
                    .filter(item->item.getPostId().equals(post1.getPostId()))
                    .findAny()
                    .isPresent())
            {
                post1.setIsLiked(1);
            }


            if(listComment.stream()
                    .filter(item->item.getPostId().equals(post1.getPostId()))
                    .findAny()
                    .isPresent())
            {
                post1.setIsComment(1);
            }
        }
        return WebKit.getPageData(list);
    }


    /**
     * 查询帖子内容列表
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("帖子详情")
    @ValidateToken(isValidate = false)
    @GetMapping(value = "/info/{postId}")
    public Post getInfo(@PathVariable("postId") Long postId) {
        Post post = postService.selectPostByIdApi(postId);
        List<Like> listUser = new ArrayList<>();

        List<Like> listLike = new ArrayList<>();

        List<Like> listComment = new ArrayList<>();
        if(apiTokenService.getLoginUser() != null){
            Like like = new Like();
            like.setType(3L);
            like.setUid(apiTokenService.getLoginUserId());
            like.setPostId(post.getUid());
            listUser = likeService.selectLikeList(like);


            like.setType(1L);
            like.setPostId(postId);
            listLike = likeService.selectLikeList(like);

            like.setType(2L);
            like.setPostId(postId);
            listComment = likeService.selectLikeList(like);
        }
        if(listUser != null && listUser.size() > 0){
            post.setIsFollow(1);
        }

        if(listLike != null && listLike.size() > 0){
            post.setIsLiked(1);
        }

        if(listComment != null && listComment.size() > 0){
            post.setIsComment(1);
        }

        //下面是进行评价列表的操作
        Comment comment = new Comment();
        comment.setPostId(postId);
        comment.setParentCommentId(0L);
        List<Comment> commentList = commentService.selectCommentList(comment);
        post.setCommentList(commentList);
        return post;
    }




    /**
     * 查询帖子内容列表
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("帖子评级")
    @ValidateToken(isValidate = false)
    @GetMapping(value = "/comment/{postId}")
    public List<Comment> comment(@PathVariable("postId") Long postId) {
        //下面是进行评价列表的操作
        Comment comment = new Comment();
        comment.setPostId(postId);
        comment.setParentCommentId(0L);
        List<Comment> commentList = commentService.selectCommentList(comment);
        return commentList;
    }



    /**
     * 查询帖子内容列表
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("我的人脉")
    @GetMapping(value = "/mySelect")
    public UserCount mySelect() {
        return commentService.selectCount(apiTokenService.getLoginUserId());
    }




    /**
     * 查询帖子内容列表
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("个人主页数据")
    @GetMapping(value = "/mineData/{uid}")
    public UserCount mineData(@PathVariable("uid") Long uid) {

        return commentService.selectCount(uid);
    }



    /**
     * 查询帖子内容列表
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("查看个人主页发布")
    @ValidateToken(isValidate = false)
    @GetMapping("/findMineList")
    public List<Post> findMineList(Post post) {

        List<Like> listUser = new ArrayList<>();

        List<Like> listLike = new ArrayList<>();

        List<Like> listComment = new ArrayList<>();
        post.setBreed(0);
        post.setDrafts(1);
        if(apiTokenService.getLoginUser() != null){
            Like like = new Like();
            like.setType(3L);
            like.setUid(apiTokenService.getLoginUserId());
            listUser = likeService.selectLikeList(like);


            like.setType(1L);
            like.setUid(apiTokenService.getLoginUserId());
            listLike = likeService.selectLikeList(like);

            like.setType(2L);
            like.setUid(apiTokenService.getLoginUserId());
            listComment = likeService.selectLikeList(like);
        }
        List<Post> list = postService.selectPostListAll(post);
        for (Post post1 : list) {
            if(listUser.stream()
                    .filter(item->item.getPostId().equals(post1.getUid()))
                    .findAny()
                    .isPresent())
            {
                post1.setIsFollow(1);
            }

            if(listLike.stream()
                    .filter(item->item.getPostId().equals(post1.getPostId()))
                    .findAny()
                    .isPresent())
            {
                post1.setIsLiked(1);
            }


            if(listComment.stream()
                    .filter(item->item.getPostId().equals(post1.getPostId()))
                    .findAny()
                    .isPresent())
            {
                post1.setIsComment(1);
            }
        }
        return list;
    }


    /**
     * 删除帖子内容
     */
    @DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds) {
        return WebKit.toAjax(postService.deletePostByIds(postIds));
    }

}